//
// Created by lingzhitech on 25-7-31.
//
#include <gtest/gtest.h>
#include "../material/HandleInput.h"
TEST(ch3, test01addliteralsandstrings) {
    // std::string s1 = "hello" + "world";
}

void readstring() {
    std::string s;
    while (std::cin >> s) {
        std::cout << s << std::endl;
    }
}

TEST(ch3, test02exercise0302) {
    handle_input("string1\napple\npig", readstring);
}

void concatestrings() {
    std::string s;
    std::string strings;
    while (getline(std::cin, s)) {
        if (!strings.empty()) {
            strings += " ";
        }
        strings += s;
    }
    std::cout << strings << std::endl;
}

TEST(ch3, test04exercise0304) {
    handle_input("table\nhello\ndavid\n", concatestrings);
}

TEST(ch3, test05exercise0306) {
    std::string s = "abcdefghijk";
    for (auto &a: s) {
        a = 'x';
    }
    std::cout << s << std::endl;
}

TEST(ch3, test06exercise0307_1) {
    std::string s = "abcdefghijk";
    decltype(s.size()) i = 0;
    while (i < s.size()) {
        s[i++] = 'x';
    }
    std::cout << s << std::endl;
}

TEST(ch3, test07exercise0308_2) {
    std::string s = "abcdefghijk";
    for (std::string::size_type i = 0; i < s.size(); i++) {
        s[i] = 'x';
    }
    std::cout << s << std::endl;
}

void deletepunct() {
    std::string s;
    getline(std::cin, s);
    std::string news;
    for (auto &a: s) {
        if (!ispunct(a)) {
            news.push_back(a);
        }
    }
    std::cout << news << std::endl;
}

TEST(ch3, test08exercise0310) {
    handle_input("abc d,|cc  c", deletepunct);
}

void loadToVector() {
    int a;
    std::vector<int> v;
    while (std::cin >> a) {
        v.push_back(a);
    }
    for (auto &a: v) {
        std::cout << a << std::endl;
    }
}

TEST(ch3, test09exercise0314) {
    handle_input("12 34 2 31", loadToVector);
}

void loadanduppercase() {
    std::string s;
    std::vector<std::string> v;
    while (std::cin >> s) {
        v.push_back(s);
    }
    for (auto &a: v) {
        for (auto &b: a) {
            b = std::toupper(b);
        }
    }
    for (std::string::size_type i = 0; i < v.size(); i++) {
        if (i % 3 == 0) {
            std::cout << std::endl;
        }
        std::cout << v[i] << " ";
    }
}

TEST(ch3, test09exercise0317) {
    handle_input("apple banana grape watermelon desk paper yellow blue person dog cat fish man", loadanduppercase);
}

void addtoVector() {
    int a;
    std::vector<int> v;
    while (std::cin >> a) {
        v.push_back(a);
    }
    for (std::string::size_type i = 0, j = 1; j < v.size(); i += 2, j += 2) {
        std::cout << v[i] + v[j] << " ";
    }
    std::cout << std::endl;
    for (std::string::size_type i = 0; i < v.size() && i < v.size() - 1 - i; i++) {
        std::cout << v[i] + v[v.size() - 1 - i] << " ";
    }
}

TEST(ch3, test09exercise0320) {
    handle_input("1 2 3 3 4 5 6 7 9", addtoVector);
}

TEST(ch3, test10exercise0323) {
    std::vector<int> nums{1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (auto it = nums.begin(); it != nums.end(); it++) {
        *it *= 2;
    }
    for (auto &a: nums) {
        std::cout << a << " ";
    }
}

TEST(ch3, test11erroraboutdefaultinitialized) {
    const int i = 9;
    int a[i];
    std::cout << a[0] << " ";
}

TEST(ch3, test12array) { {
        char a1[] = {'C', '+', '+'}; // list initialization, no null
        char a2[] = {'C', '+', '+', '\0'}; // list initialization, explicit null
        char a3[] = "C++"; // null terminator added automatically
        // const char a4[6] = "Daniel"; // error: no space for the null
    } {
        int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // int a2[] = a;//can't initialize local variable 'a2' of type int[] with int[9]
        int a3[9] = {0};
        //a3 = a;// Cannot assign to an array type int[9]
    }
}

std::string sa[10];
int ia[10];
TEST(ch3, test13exercise0328) {
    std::string sa2[10];
    int ia2[10];

    std::cout << ia[0] << " " << ia2[0] << std::endl;
    //这里sa和sa2应该都是被初始化为0
}

void arrayuninitialize() {
    unsigned scores[11]; // 11 buckets, all value initialized to 0
    unsigned grade;
    while (std::cin >> grade) {
        if (grade <= 100)++scores[grade / 10]; // increment the counter for the current cluster
    }
    for (auto &a: scores) {
        std::cout << a << " ";
    }
    std::cout << std::endl;
}

TEST(ch3, test14exercise0333) {
    handle_input("1 23 93 29 48 13 83 21 29 49 84 72 12", arrayuninitialize);
}
